package com.tanhua.server.controller;

import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.VideoVO;
import com.tanhua.server.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author liuyp
 * @date 2021/02/02
 */
@RestController
@RequestMapping("/smallVideos")
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 视频-发布视频
     * POST /smallVideos
     */
    @PostMapping
    public ResponseEntity saveVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        return videoService.saveVideo(videoFile, videoThumbnail);
    }

    /**
     * 视频-小视频列表
     * GET /smallVideos
     */
    @GetMapping
    public ResponseEntity findVideoList(@RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10") int pagesize){
        if (page <= 0) {
            page = 1;
        }
        PageResult<VideoVO> pageResult = videoService.findVideoList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 视频-关注作者
     * POST /smallVideos/:uid/userFocus
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity followUser(@PathVariable("id") Long targetUserId){
        return videoService.followUser(targetUserId);
    }

    /**
     * 视频-取消关注
     * POST /smallVideos/:uid/userUnFocus
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity unfollowUser(@PathVariable("id") Long targetUserId){
        return videoService.unfollowUser(targetUserId);
    }
}
